// 微信小程序 behavior
// 用于给自定义组件添加success和fail方法，避免在页面中额外给组件添加监听回调方法
//let component = this.selectComponent('选择器')
// component.$$showComponent({
//   data: {},
//   success () {},
//   fail () {}
// })

const voidFunction = () => {}

module.exports = Behavior({
  data: {
    componentSuccess: voidFunction,
    componentFail: voidFunction,
    componentComplete: voidFunction
  },
  lifetimes: {
    attached() {
      // console.log('behavior attached');
    },
    detached() {
      // console.log('behavior detached');
    }
  },
  methods: {
    $$showComponent(options = {}) {
      let {
        success,
        fail,
        complete,
        ...otherOptions
      } = options
      this.setData({
        showComponent: true,
        componentSuccess: success || voidFunction,
        componentFail: fail || voidFunction,
        componentComplete: complete || voidFunction
      })
      let keys = Object.keys(otherOptions)
      if (keys.length == 1 && keys[0] == 'data') {
        otherOptions = otherOptions.data
      }
      this.showComponent(otherOptions)
    },
    $$success(e) {
      this.data.componentSuccess(e)
    },
    $$fail(e) {
      this.data.componentFail(e)
    },
    $$complete(e) {
      this.data.componentComplete(e)
    }
  }
})